﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using GWS.Entities;

namespace GWS.DAL
{
    public class PatientAccess
    {
        private static Patient patientFlds(DataRow Row)
        {
            Patient p = new Patient();
            p.ID = (int)Row["patient_id"];
            p.UserID = Row["patients_user_id"] as string;
            p.FirstName = Row["patient_first_name"] as string;
            p.LastName = Row["patient_last_name"] as string;
            p.Address = Row["patient_addr"] as string;
            p.City = Row["patient_city"] as string;
            p.Zip = Row["patient_zip"] as string;
            p.State = Row["patient_state"] as string;
            p.Phone = Row["patient_phone"] as string;
            p.Healthcare = Convert.ToBoolean(Row["patient_healthcare"].ToString());
            p.Diagnosis = Row["patient_diagnosis"] as string;
            return p;
        }

        private static Patient[] DataSetToPatientssArray(DataSet ds)
        {
            Patient[] patients = new Patient[ds.Tables[0].Rows.Count];
            for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
            {
                patients[i] = patientFlds(ds.Tables[0].Rows[i]);
            }
            return patients;
        }

        public static Patient[] GetAllPatients()
        {
            return DataSetToPatientssArray(SqlHelper.ExecuteDataset(Global.ConnectionString, "sprocGetAllPatients"));
        }

        public static Patient GetPatient(string UserID)
        {
            DataSet dr = SqlHelper.ExecuteDataset(Global.ConnectionString, "sprocGetPatient", UserID);
            return patientFlds(dr.Tables[0].Rows[0]);
        }

        public static Patient GetPatientByID(string PatientID)
        {
            DataSet dr = SqlHelper.ExecuteDataset(Global.ConnectionString, "sprocGetPatientByID", PatientID);
            return patientFlds(dr.Tables[0].Rows[0]);
        }

        public static int ImportPeople(Patient[] people)
        {
            int x = 0;
            foreach (Patient person in people)
            {
                int temp = SqlHelper.ExecuteNonQuery(Global.ConnectionString, "sprocAddPatient",
                person.FirstName, person.LastName, person.Address, person.City, person.State,
                person.Zip, person.Phone, person.Healthcare, person.Diagnosis, person.UserID);
                if (temp > 0)
                    x = x + 1;
            }
            return x;
        }

        public static bool AddPatient(Patient person)
        {
            int temp = SqlHelper.ExecuteNonQuery(Global.ConnectionString, "sprocAddPatient",
                person.FirstName, person.LastName, person.Address, person.City, person.State,
                person.Zip, person.Phone, person.Healthcare, person.Diagnosis, person.UserID);
            if (temp > 0)
                return true;

            return false;
        }

        public static bool UpdatePatient(Patient person)
        {
            int temp = SqlHelper.ExecuteNonQuery(Global.ConnectionString, "sprocUpdatePatient",
                person.FirstName, person.LastName, person.Address, person.City, person.State,
                person.Zip, person.Phone, person.Healthcare, person.UserID);
            if (temp > 0)
                return true;

            return false;
        }
    }
}
